package com.example.it.testformartialdesign;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

public class FruitActivity extends AppCompatActivity {

    public static final String FRUIT_NAME = "fruit_name";
    public static final String FRUIT_IMAGE_ID = "fruit_image_id";
    private Toolbar toolbar;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private ImageView iv_fruit;
    private TextView furitContent;

    private void initFindView() {
        toolbar = (Toolbar) findViewById(R.id.fruit_toolbar);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        iv_fruit = (ImageView) findViewById(R.id.iv_fruit);
        furitContent = (TextView) findViewById(R.id.fruit_cntent_txt);
    }

    private void initActionBar(String name) {
        this.setSupportActionBar(toolbar);
        ActionBar actionBar = this.getSupportActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);
        //设置显示的标题
        this.collapsingToolbarLayout.setTitle(name);
    }

    private void initImage(int imgId) {
        Glide.with(this).load(imgId).into(iv_fruit);
    }

    private String generateFruitContent(String name) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < 500; i++) {
            buffer.append(name);
        }

        return buffer.toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit);


        Intent intent = getIntent();
        String fruitName = intent.getStringExtra(FRUIT_NAME);
        int fruitImageId = intent.getIntExtra(FRUIT_IMAGE_ID, 0);

        initFindView();
        initActionBar(fruitName);
        if (fruitImageId != 0)
            initImage(fruitImageId);

        this.furitContent.setText(generateFruitContent(fruitName));
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onNavigateUp();
    }
}
